var e = function () {
  return (e =
    Object.assign ||
    function (e) {
      for (var n, t = 1, r = arguments.length; t < r; t++)
        for (var o in (n = arguments[t]))
          Object.prototype.hasOwnProperty.call(n, o) && (e[o] = n[o]);
      return e;
    }).apply(this, arguments);
};
function n(e, n, t, r) {
  return new (t || (t = Promise))(function (o, a) {
    function i(e) {
      try {
        s(r.next(e));
      } catch (e) {
        a(e);
      }
    }
    function c(e) {
      try {
        s(r.throw(e));
      } catch (e) {
        a(e);
      }
    }
    function s(e) {
      e.done
        ? o(e.value)
        : new t(function (n) {
            n(e.value);
          }).then(i, c);
    }
    s((r = r.apply(e, n || [])).next());
  });
}
function t(e, n) {
  var t,
    r,
    o,
    a,
    i = {
      label: 0,
      sent: function () {
        if (1 & o[0]) throw o[1];
        return o[1];
      },
      trys: [],
      ops: [],
    };
  return (
    (a = { next: c(0), throw: c(1), return: c(2) }),
    'function' == typeof Symbol &&
      (a[Symbol.iterator] = function () {
        return this;
      }),
    a
  );
  function c(a) {
    return function (c) {
      return (function (a) {
        if (t) throw new TypeError('Generator is already executing.');
        for (; i; )
          try {
            if (
              ((t = 1),
              r &&
                (o =
                  2 & a[0]
                    ? r.return
                    : a[0]
                    ? r.throw || ((o = r.return) && o.call(r), 0)
                    : r.next) &&
                !(o = o.call(r, a[1])).done)
            )
              return o;
            switch (((r = 0), o && (a = [2 & a[0], o.value]), a[0])) {
              case 0:
              case 1:
                o = a;
                break;
              case 4:
                return i.label++, { value: a[1], done: !1 };
              case 5:
                i.label++, (r = a[1]), (a = [0]);
                continue;
              case 7:
                (a = i.ops.pop()), i.trys.pop();
                continue;
              default:
                if (
                  !(o = (o = i.trys).length > 0 && o[o.length - 1]) &&
                  (6 === a[0] || 2 === a[0])
                ) {
                  i = 0;
                  continue;
                }
                if (3 === a[0] && (!o || (a[1] > o[0] && a[1] < o[3]))) {
                  i.label = a[1];
                  break;
                }
                if (6 === a[0] && i.label < o[1]) {
                  (i.label = o[1]), (o = a);
                  break;
                }
                if (o && i.label < o[2]) {
                  (i.label = o[2]), i.ops.push(a);
                  break;
                }
                o[2] && i.ops.pop(), i.trys.pop();
                continue;
            }
            a = n.call(e, i);
          } catch (e) {
            (a = [6, e]), (r = 0);
          } finally {
            t = o = 0;
          }
        if (5 & a[0]) throw a[1];
        return { value: a[0] ? a[1] : void 0, done: !0 };
      })([a, c]);
    };
  }
}
var r = (function () {
  function e() {}
  return (
    (e.add = function (n) {
      e._handleList.push(n), window.addEventListener('message', n, !1);
    }),
    (e.remove = function (n) {
      var t = e._handleList.indexOf(n);
      t >= 0 && e._handleList.splice(t, 1),
        window.removeEventListener('message', n, !1);
    }),
    (e.empty = function () {
      for (; e._handleList.length; )
        window.removeEventListener('message', e._handleList.shift(), !1);
    }),
    (e.parse = function (e) {
      return 'object' == typeof e ? e : JSON.parse(e);
    }),
    (e._handleList = []),
    e
  );
})();
var o,
  a,
  i,
  c = this;
!(function (e) {
  (e.unknown = 'unknown'),
    (e.spreadsheet = 's'),
    (e.writer = 'w'),
    (e.presentation = 'p'),
    (e.pdf = 'f');
})(o || (o = {})),
  (function (e) {
    (e.wps = 'w'), (e.et = 's'), (e.presentation = 'p'), (e.pdf = 'f');
  })(a || (a = {})),
  (function (e) {
    (e.nomal = 'nomal'), (e.simple = 'simple');
  })(i || (i = {}));
var s,
  u,
  p =
    ((s = 0),
    function () {
      return ++s;
    }),
  f =
    ((u = null),
    function (e, n) {
      if (!u) {
        u = document.createElement('iframe');
        var t = {
          id: 'office-iframe',
          src: e,
          scrolling: 'no',
          frameborder: '0',
          allowfullscreen: 'allowfullscreen',
          webkitallowfullscreen: 'true',
          mozallowfullscreen: 'true',
          className: 'web-office-iframe',
        };
        for (var r in t) u.setAttribute(r, t[r]);
        n ? n.appendChild(u) : document.body.appendChild(u),
          (u.destroy = function () {
            u.parentNode.removeChild(u), (u = null);
          });
      }
      return u;
    }),
  l = function (e) {
    f().contentWindow.postMessage(JSON.stringify(e), '*');
  },
  d = function (e) {
    return new Promise(function (n) {
      var t = p();
      e.type = O();
      var o = function (e) {
        var a = r.parse(e.data);
        'wps.api.reply' === a.eventName &&
          a.msgId === t &&
          (n(a.data), r.remove(o));
      };
      r.add(o), l({ eventName: 'wps.jssdk.api', data: e, msgId: t });
    });
  },
  v = [],
  b = [],
  m = !1,
  h = !1,
  w = !1,
  j = function () {
    (v = []), (b = []), (m = !1), (h = !1), (w = !1);
  },
  y = function (o, a, i) {
    return n(c, void 0, void 0, function () {
      var c, s, u, f, d, j, y, g, I, k;
      return t(this, function (_) {
        return (
          (c = m ? b : v),
          (s = p()),
          (a.type = O()),
          (d = new Promise(function (e, n) {
            (u = e), (f = n);
          })),
          (j = {}),
          a.args &&
            ((y = (function (e) {
              var n = {};
              return [
                (e = e.map(function (e) {
                  if (
                    (function (e) {
                      if (!e) return !1;
                      for (var n = e; null !== Object.getPrototypeOf(n); )
                        n = Object.getPrototypeOf(n);
                      return Object.getPrototypeOf(e) === n;
                    })(e)
                  ) {
                    var t = {};
                    for (var r in e) {
                      var o = e[r];
                      if ('function' == typeof o) {
                        var a = p();
                        (n[a] = o), (o = { callbackId: a });
                      }
                      t[r] = o;
                    }
                    return t;
                  }
                  return 'function' == typeof e
                    ? ((a = p()), (n[a] = e), { callbackId: a })
                    : e;
                })),
                n,
              ];
            })(a.args)),
            (g = y[0]),
            (I = y[1]),
            (a.args = g),
            (j = I)),
          (k = function () {
            var e = this,
              a = function (p) {
                return n(e, void 0, void 0, function () {
                  var e, n, d;
                  return t(this, function (t) {
                    switch (t.label) {
                      case 0:
                        return 'api.callback' ===
                          (e = r.parse(p.data)).eventName &&
                          e.callbackId &&
                          j[e.callbackId]
                          ? ((m = !0),
                            [4, j[e.callbackId].apply(j, e.data.args)])
                          : [3, 2];
                      case 1:
                        (n = t.sent()),
                          l({
                            eventName: 'api.callback.reply',
                            result: n,
                            callbackId: e.callbackId,
                          }),
                          (t.label = 2);
                      case 2:
                        if (e.eventName === o + '.reply' && e.msgId === s) {
                          if (e.error) {
                            for (
                              (d = new Error('')).stack = e.error + '\n' + i;
                              c.length;

                            )
                              c.shift();
                            m ? (w = !1) : (h = !1), f(d);
                          } else u(e.result);
                          r.remove(a);
                        }
                        return [2];
                    }
                  });
                });
              };
            return r.add(a), d;
          }),
          c.push([{ eventName: o, data: a, msgId: s }, k]),
          (function r(o) {
            return n(this, void 0, void 0, function () {
              var n, a, i, c;
              return t(this, function (t) {
                switch (t.label) {
                  case 0:
                    if (((n = m), (m ? w : h) || !o.length)) return [3, 4];
                    m ? (w = !0) : (h = !0), (t.label = 1);
                  case 1:
                    return o.length
                      ? ((a = o.shift()),
                        (i = a[0]),
                        (c = a[1]),
                        'function' == typeof (i = e({}, i)).data &&
                          (i.data = i.data()),
                        l(i),
                        [4, c()])
                      : [3, 3];
                  case 2:
                    return t.sent(), [3, 1];
                  case 3:
                    return (
                      m ? (w = !1) : (h = !1), n && (m = !1), [2, r(n ? v : b)]
                    );
                  case 4:
                    return [2];
                }
              });
            });
          })(c),
          [2, d]
        );
      });
    });
  };
var g,
  I,
  k = function (n, t) {
    void 0 === t && (t = !0);
    var r = e({}, n),
      o = r.headers,
      a = void 0 === o ? {} : o,
      c = r.subscriptions,
      s = void 0 === c ? {} : c,
      u = r.mode,
      p = void 0 === u ? i.nomal : u,
      f = a.backBtn,
      l = void 0 === f ? {} : f,
      d = a.shareBtn,
      v = void 0 === d ? {} : d,
      b = a.otherMenuBtn,
      m = void 0 === b ? {} : b,
      h = function (e, n) {
        e.subscribe &&
          'function' == typeof e.subscribe &&
          ((e.callback = n), (s[n] = e.subscribe), t && delete e.subscribe);
      };
    if (
      (h(l, 'wpsconfig_back_btn'),
      h(v, 'wpsconfig_share_btn'),
      h(m, 'wpsconfig_other_menu_btn'),
      m.items && Array.isArray(m.items))
    ) {
      var w = [];
      m.items.forEach(function (e, n) {
        switch ((void 0 === e && (e = {}), e.type)) {
          case 'export_img':
            (e.type = 1), (e.callback = 'export_img');
            break;
          case 'export_pdf':
            (e.type = 1), (e.callback = 'export_pdf');
            break;
          case 'save_version':
            (e.type = 1), (e.callback = 'save_version');
            break;
          case 'about_wps':
            (e.type = 1), (e.callback = 'about_wps');
            break;
          case 'split_line':
            e.type = 2;
            break;
          case 'custom':
            (e.type = 3), h(e, 'wpsconfig_other_menu_btn_' + n), w.push(e);
        }
      }),
        w.length && (x || E) && (m.items = w);
    }
    if (
      (p === i.simple &&
        (r.wpsUrl && (r.wpsUrl += '&simple&hidecmb'),
        r.url && (r.url += '&simple&hidecmb')),
      r.debug &&
        (r.wpsUrl && (r.wpsUrl += '&debugger'),
        r.url && (r.url += '&debugger')),
      r.wordOptions && (r.wpsOptions = r.wordOptions),
      r.excelOptions && (r.etOptions = r.excelOptions),
      r.pptOptions && (r.wppOptions = r.pptOptions),
      'object' == typeof s.print)
    ) {
      var j = 'wpsconfig_print';
      'function' == typeof s.print.subscribe &&
        ((s[j] = s.print.subscribe),
        (r.print = { callback: j }),
        void 0 !== s.print.custom && (r.print.custom = s.print.custom)),
        delete s.print;
    }
    'function' == typeof s.exportPdf &&
      ((s[(j = 'wpsconfig_export_pdf')] = s.exportPdf),
      (r.exportPdf = { callback: j }),
      delete s.exportPdf);
    return e({}, r, { subscriptions: s });
  },
  O =
    ((g = ''),
    function (e) {
      if ((void 0 === e && (e = ''), !g && e)) {
        var n = e.toLowerCase();
        -1 !== n.indexOf('/office/s/') && (g = o.spreadsheet),
          -1 !== n.indexOf('/office/w/') && (g = o.writer),
          -1 !== n.indexOf('/office/p/') && (g = o.presentation),
          -1 !== n.indexOf('/office/f/') && (g = o.pdf);
      }
      if (!g) {
        var t = e.match(/[\?&]type=([a-z]+)/) || [];
        g = a[t[1]] || '';
      }
      return g;
    }),
  _ = window.navigator.userAgent.toLowerCase(),
  x = /Android|webOS|iPhone|iPod|BlackBerry|iPad/i.test(_),
  E = (function () {
    try {
      return (
        -1 !== window._parent.location.search.indexOf('from=wxminiprogram')
      );
    } catch (e) {
      return !1;
    }
  })();
function P(e) {
  return (
    (e = e || Object.create(null)),
    {
      on: function (n, t) {
        (e[n] || (e[n] = [])).push(t);
      },
      off: function (n, t) {
        e[n] && e[n].splice(e[n].indexOf(t) >>> 0, 1);
      },
      emit: function (n, t) {
        (e[n] || []).slice().map(function (e) {
          e(t);
        }),
          (e['*'] || []).slice().map(function (e) {
            e(n, t);
          });
      },
    }
  );
}
!(function (e) {
  (e[(e.msoFalse = 0)] = 'msoFalse'), (e[(e.msoTrue = -1)] = 'msoTrue');
})(I || (I = {}));
var N = 0,
  C = new Set();
function A(e) {
  return (
    ++N,
    !e &&
      (function (e) {
        C.forEach(function (n) {
          return n(e);
        });
      })(N),
    N
  );
}
function S(a, i) {
  var c,
    s = this,
    u = i.Events,
    p = i.Enum,
    f = i.Props,
    d = f[0],
    v = f[1],
    b = { objId: N };
  switch (
    ((function n(t, r, o) {
      r = r.slice(0);
      var a = function () {
        var a = r.shift();
        !a.alias &&
          ~L.indexOf(a.prop) &&
          r.push(e({}, a, { alias: a.prop + 'Async' })),
          Object.defineProperty(t, a.alias || a.prop, {
            get: function () {
              var r = 1 === a.cache,
                i = r && this['__' + a.prop + 'CacheValue'];
              if (i) return i;
              var c = new Error(''),
                s = c.stack.split('\n').slice(2).join('\n'),
                u = A(r),
                p = function () {
                  for (var n, r = [], i = 0; i < arguments.length; i++)
                    r[i] = arguments[i];
                  void 0 !== a.caller
                    ? (function n(t, r, o) {
                        r = r.slice(0);
                        var a = function () {
                          var a = r.shift();
                          !a.alias &&
                            ~L.indexOf(a.prop) &&
                            r.push(e({}, a, { alias: a.prop + 'Async' })),
                            Object.defineProperty(t, a.alias || a.prop, {
                              get: function () {
                                var e = 1 === a.cache,
                                  r = e && this['__' + a.prop + 'CacheValue'];
                                if (r) return r;
                                var i = new Error(''),
                                  c = i.stack.split('\n').slice(2).join('\n'),
                                  s = A(e),
                                  u = function () {
                                    for (
                                      var e, r = [], i = 0;
                                      i < arguments.length;
                                      i++
                                    )
                                      r[i] = arguments[i];
                                    void 0 !== a.caller
                                      ? n((e = { objId: A() }), o[a.caller], o)
                                      : (e = {});
                                    return (
                                      T(
                                        e,
                                        'api.caller',
                                        function () {
                                          return {
                                            obj: u,
                                            parentObjId: t.objId,
                                            objId: e.objId || -1,
                                            args: r,
                                            prop: a.prop,
                                          };
                                        },
                                        c
                                      ),
                                      e
                                    );
                                  };
                                return (
                                  (u.objId = -1),
                                  void 0 !== a.getter &&
                                    ((u.objId = s), n(u, o[a.getter], o)),
                                  T(
                                    u,
                                    'api.getter',
                                    function () {
                                      return {
                                        parentObjId: t.objId,
                                        objId: u.objId,
                                        prop: a.prop,
                                      };
                                    },
                                    c
                                  ),
                                  e && (this['__' + a.prop + 'CacheValue'] = u),
                                  u
                                );
                              },
                              set: function (e) {
                                var n = new Error(''),
                                  r = n.stack.split('\n').slice(2).join('\n');
                                T(
                                  {},
                                  'api.setter',
                                  function () {
                                    return {
                                      parentObjId: t.objId,
                                      objId: -1,
                                      prop: a.prop,
                                      value: e,
                                    };
                                  },
                                  r
                                );
                              },
                            });
                        };
                        for (; r.length; ) a();
                      })((n = { objId: A() }), o[a.caller], o)
                    : (n = {});
                  return (
                    T(
                      n,
                      'api.caller',
                      function () {
                        return {
                          obj: p,
                          parentObjId: t.objId,
                          objId: n.objId || -1,
                          args: r,
                          prop: a.prop,
                        };
                      },
                      s
                    ),
                    n
                  );
                };
              return (
                (p.objId = -1),
                void 0 !== a.getter && ((p.objId = u), n(p, o[a.getter], o)),
                T(
                  p,
                  'api.getter',
                  function () {
                    return {
                      parentObjId: t.objId,
                      objId: p.objId,
                      prop: a.prop,
                    };
                  },
                  s
                ),
                r && (this['__' + a.prop + 'CacheValue'] = p),
                p
              );
            },
            set: function (e) {
              var n = new Error(''),
                r = n.stack.split('\n').slice(2).join('\n');
              T(
                {},
                'api.setter',
                function () {
                  return {
                    parentObjId: t.objId,
                    objId: -1,
                    prop: a.prop,
                    value: e,
                  };
                },
                r
              );
            },
          });
      };
      for (; r.length; ) a();
    })(b, d, v),
    (b.Events = u),
    (b.Enum = p),
    (a.Enum = b.Enum),
    (a.Events = b.Events),
    O(a.url))
  ) {
    case o.writer:
      a.WordApplication = a.WpsApplication = function () {
        return b;
      };
      break;
    case o.spreadsheet:
      a.ExcelApplication = a.EtApplication = function () {
        return b;
      };
      break;
    case o.presentation:
      a.PPTApplication = a.WppApplication = function () {
        return b;
      };
      break;
    case o.pdf:
      a.PDFApplication = function () {
        return b;
      };
  }
  (a.Application = b),
    (a.Free = function (e) {
      return y('api.free', { objId: e }, '');
    }),
    (a.Stack = b.Stack =
      ((c = function (e) {
        return a.Free(e);
      }),
      function () {
        var e = [],
          n = function (n) {
            e.push(n);
          };
        return (
          C.add(n),
          {
            End: function () {
              c(e), C.delete(n);
            },
          }
        );
      }));
  var h = {};
  r.add(function (e) {
    return n(s, void 0, void 0, function () {
      var n, o, a, i, c;
      return t(this, function (t) {
        return (
          'api.event' === (n = r.parse(e.data)).eventName &&
            n.data &&
            ((o = n.data),
            (a = o.eventName),
            (i = o.data),
            o.objIds,
            (c = h[a]) && ((m = !0), c(i))),
          [2]
        );
      });
    });
  }),
    (b.Sub = {}),
    Object.values(u).forEach(function (e) {
      Object.defineProperty(b.Sub, e, {
        set: function (n) {
          (h[e] = n),
            l({
              eventName: 'api.event.register',
              data: { eventName: e, register: !!n, objId: ++N },
            });
        },
      });
    });
}
var B,
  L = [
    'ExportAsFixedFormat',
    'GetOperatorsInfo',
    'ImportDataIntoFields',
    'ReplaceText',
    'ReplaceBookmark',
    'GetBookmarkText',
    'GetComments',
  ];
function D(n, t, r) {
  t = t.slice(0);
  var o = function () {
    var o = t.shift();
    if (!o.alias && ~L.indexOf(o.prop)) {
      t.push(e({}, o, { alias: o.prop + 'Async' }));
    }
    Object.defineProperty(n, o.alias || o.prop, {
      get: function () {
        var e = o.cache === 1;
        var t = e && this['__' + o.prop + 'CacheValue'];
        if (!t) {
          var a = new Error('');
          var i = a.stack.split('\n').slice(2).join('\n');
          var c = A(e);
          var s = function () {
            for (var e = [], t = 0, a; t < arguments.length; t++) {
              e[t] = arguments[t];
            }
            if (o.caller !== undefined) {
              a = { objId: A() };
              D(a, r[o.caller], r);
            } else {
              a = {};
            }
            return (
              T(
                a,
                'api.caller',
                function () {
                  return {
                    obj: u,
                    parentObjId: n.objId,
                    objId: a.objId || -1,
                    args: e,
                    prop: o.prop,
                  };
                },
                i
              ),
              a
            );
          };
          var u = s;
          u.objId = -1;
          if (o.getter !== undefined) {
            u.objId = c;
            D(u, r[o.getter], r);
          }
          T(
            u,
            'api.getter',
            function () {
              return { parentObjId: n.objId, objId: u.objId, prop: o.prop };
            },
            i
          );
          if (e) {
            this['__' + o.prop + 'CacheValue'] = u;
          }
          return u;
        } else {
          return t;
        }
      },
      set: function (e) {
        var t = new Error('');
        var r = t.stack.split('\n').slice(2).join('\n');
        T(
          {},
          'api.setter',
          function () {
            return { parentObjId: n.objId, objId: -1, prop: o.prop, value: e };
          },
          r
        );
      },
    });
  };
  while (t.length) {
    o();
  }
}
function T(e, n, t, r) {
  var o = y(n, t, r).then(function (n) {
    return n && n.objId ? ((e.then = void 0), (e.objId = n.objId), e) : n;
  });
  (e.then = function (e, n) {
    return o.then(e, n);
  }),
    (e.Destroy = function () {
      return y('api.free', { objId: e.objId }, '');
    });
}
var F = 'fileOpen',
  V = function (e) {
    return 'api.ready' === e;
  },
  R = function (e, n) {
    void 0 === e && (e = {});
    r.add(function (t) {
      var o = r.parse(t.data),
        a = o.eventName,
        i = void 0 === a ? '' : a,
        c = o.data,
        s = void 0 === c ? null : c,
        u = o.url,
        p = void 0 === u ? null : u;
      -1 === ['wps.jssdk.api'].indexOf(i) &&
        ('ready' === i
          ? (l({ eventName: 'setConfig', data: e }),
            B.tokenData && l({ eventName: 'setToken', data: B.tokenData }),
            B.commandBars &&
              l({ eventName: 'setCommandBars', data: B.commandBars }),
            (B.iframeReady = !0))
          : 'open.result' === i
          ? B.emit(F, s)
          : 'api.ready' === i && S(B, s),
        'function' == typeof n[i] && n[i](B, p || s));
    });
  };
function U(n) {
  void 0 === n && (n = {}), B && B.destroy();
  try {
    var t,
      o = k(n),
      a = o.wpsUrl,
      i = o.subscriptions,
      c = void 0 === i ? {} : i,
      s = o.mount,
      u = void 0 === s ? null : s,
      p = o.url,
      v = a || p,
      b = f(v, u);
    return (
      delete o.mount,
      a && delete o.wpsUrl,
      p && delete o.url,
      delete o.subscriptions,
      (B = {
        url: v,
        version: '1.1.0',
        iframe: b,
        Enum: e({}, I),
        iframeReady: !1,
        tokenData: null,
        commandBars: null,
        setToken: function (e) {
          (B.tokenData = e),
            B.iframeReady && l({ eventName: 'setToken', data: e });
        },
        ready: function () {
          return (
            t ||
            (t = new Promise(function (e) {
              var n = function (t) {
                var o = r.parse(t.data).eventName;
                V(o) && (e(), r.remove(n));
              };
              r.add(n);
            }))
          );
        },
        destroy: function () {
          b.destroy(), r.empty(), (B = null), j(), (C = new Set());
        },
        save: function () {
          return d({ api: 'save' });
        },
        setCommandBars: function (e) {
          (B.commandBars = e),
            B.iframeReady && l({ eventName: 'setCommandBars', data: e });
        },
        updateConfig: function (e) {
          void 0 === e && (e = {});
          var n = k(e, !1),
            t = n.subscriptions,
            o = void 0 === t ? {} : t;
          r.empty(), R(n, o), n && l({ eventName: 'setConfig', data: n });
        },
      }),
      (B = e({}, B, new P.prototype.constructor())),
      R(o, c),
      B.ready(),
      B
    );
  } catch (e) {
    console.error(e);
  }
}
console.log('WebOfficeSDK JS-SDK V1.1.0');
var W = Object.freeze({ Listener: R, config: U });
window.WPS = W;
var G = U;
export default { config: U };
export { G as config };
